package com.epower.bo.epowersentence.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.epower.bo.epowersentence.EpowerSentenceService;
import com.epower.dao.epowersentence.EpowerSentenceDAO;
import com.epower.dataobject.EpowerSentenceDO;

@Scope("singleton")
@Component("epowerSentenceBO")
public class EpowerSentenceServiceImpl implements EpowerSentenceService {

    private EpowerSentenceDAO epowerSentenceDAO;

    @Override
    public List<EpowerSentenceDO> querySentenceBySpelling(String spelling) {
        return epowerSentenceDAO.querySentenceBySpelling(spelling);
    }

    public EpowerSentenceDAO getEpowerSentenceDAO() {
        return epowerSentenceDAO;
    }

    @Transactional(propagation=Propagation.REQUIRED,rollbackFor = Throwable.class)
    public Integer addEpowerSentence(List<EpowerSentenceDO> epowerSentenceDOs) {
        try{
            List<EpowerSentenceDO> adds = new ArrayList<EpowerSentenceDO>();
            List<EpowerSentenceDO> updates = new ArrayList<EpowerSentenceDO>();
            for(EpowerSentenceDO temp:epowerSentenceDOs){
                if(temp.getId()>0){
                    updates.add(temp);
                }else{
                    adds.add(temp);
                }
            }
            int addnum =0;
            int update = 0;
            if (updates.size()>0) {
                update = epowerSentenceDAO.updateEpowerSentence(updates);
            }
            if (adds.size()>0) {
                addnum = epowerSentenceDAO.addEpowerSentence(adds);
            }
            return addnum+update;
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public Integer deleteEpowerSentenceBySpelling(String spelling) {
        return epowerSentenceDAO.deleteEpowerSentenceBySpelling(spelling);
    }
    
    @Resource
    public void setEpowerSentenceDAO(EpowerSentenceDAO epowerSentenceDAO) {
        this.epowerSentenceDAO = epowerSentenceDAO;
    }

    @Override
    public int querySentenceCountBySpelling(String spelling) {
        return epowerSentenceDAO.querySentenceCountBySpelling(spelling);
    }

    @Override
    public boolean deleteEpowerSentenceById(int id) {
        return epowerSentenceDAO.deleteEpowerSentenceById(id);
    }

}
